package com.jzg.sys.manager;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.jzg.sys.api.base.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


@Service("cacheService")
public class BaseCacheManager implements CacheService {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public void put(String key, Object obj, String sysCode) {
		ValueOperations<String, Object> vop = redisTemplate.opsForValue();
		vop.set(key + sysCode, obj);
	}

	@Override
	public void put(String key, Object obj, String sysCode, Long second) {
		ValueOperations<String, Object> vop = redisTemplate.opsForValue();
		vop.set(key + sysCode, obj, second, TimeUnit.SECONDS);
	}

	@Override
	public Object get(String key, String sysCode) {
		ValueOperations<String, Object> vop = redisTemplate.opsForValue();
		return vop.get(key + sysCode);
	}

	@Override
	public void remove(String key, String sysCode) {
		redisTemplate.delete(key + sysCode);
	}

	@Override
	public boolean hasKey(String key, String sysCode) {
		return redisTemplate.hasKey(key + sysCode);
	}

	@Override
	public void addSet(String key, String sysCode, Long second, Object... values) {
		SetOperations<String, Object> sop = redisTemplate.opsForSet();
		for (Object obj : values) {
			sop.add(key + sysCode, obj);
		}
	}

	@Override
	public void popSet(String key, String sysCode) {
		SetOperations<String, Object> sop = redisTemplate.opsForSet();
		sop.pop(key + sysCode);
	}

	@Override
	public boolean hasKeySet(String key, String sysCode) {
		SetOperations<String, Object> sop = redisTemplate.opsForSet();
		Long size = sop.size(key + sysCode);
		if (size == null || size == 0) {
			return false;
		}
		Set<Object> set = sop.members(key + sysCode);
		if (set.size() == 0) {
			return false;
		}
		for (Object object : set) {
			System.out.println(object.toString());
		}
		return true;
	}

	@Override
	public List<Object> randomMembers(String key, long count, String sysCode) {
		SetOperations<String, Object> sop = redisTemplate.opsForSet();
		return sop.randomMembers(key + sysCode, count);
	}

	@Override
	public Set<Object> distinctRandomMembers(String key, long count, String sysCode) {
		SetOperations<String, Object> sop = redisTemplate.opsForSet();
		return sop.distinctRandomMembers(key + sysCode, count);
	}

	public void addMap(String key, Object hashKey, String sysCode, Object value) {
		redisTemplate.opsForHash().put(key + sysCode, hashKey, value);
	}

	public boolean hasKeyMap(String key, Object hashKey, String sysCode) {
		return redisTemplate.opsForHash().hasKey(key + sysCode, hashKey);
	}

	public Object getMap(String key, Object hashKey, String sysCode) {
		return redisTemplate.opsForHash().get(key + sysCode, hashKey);
	}

	public void deleteMap(String key, String sysCode, Object... hashKeys) {
		redisTemplate.opsForHash().delete(key + sysCode, hashKeys);
	}
}
